<template>
  <div class="container">
    <!-- 顶部区域 -->
    <div class="info-box">
      <van-image
        round
        fit="cover"
        width="65"
        height="65"
        :src="userInfo.photo"
      />
      <h2 class="name">
        {{ userInfo.name }}
        <br />
        <br />
        <van-tag type="primary" color="#fff" text-color="#3296fa">{{
          userInfo.birthday
        }}</van-tag>
      </h2>
    </div>
    <!-- layout组件 /* 操作导航 */ -->
    <div class="user-nav">
      <van-row>
        <van-col span="8">
          <van-icon name="newspaper-o" color="#90c5ff" />
          我的作品
        </van-col>
        <van-col span="8">
          <van-icon name="star-o" color="#ff2645" />
          我的收藏</van-col
        >
        <van-col span="8">
          <van-icon name="tosend" color="#ffc65d" />
          阅读历史
        </van-col>
      </van-row>
    </div>
    <div class="user-group">
      <van-cell-group>
        <van-cell
          icon="edit"
          title="编辑资料"
          is-link
          @click="$router.push('/edit')"
        />
        <van-cell
          icon="chat-o"
          title="小智同学"
          is-link
          @click="$router.push('/student')"
        />
        <van-cell icon="setting-o" title="系统设置" is-link />
        <van-cell icon="warning-o" title="退出登录" is-link @click="logout" />
      </van-cell-group>
    </div>
  </div>
</template>

<script>
//导入网络中间层
import { getUserInfo } from './user.js'
import { removeItem } from '@/utils/token.js'
export default {
  name: 'user',
  data () {
    return {
      //1.添加数据
      userInfo: {}
    }
  },
  //生命钩子
  created () {
    getUserInfo().then(res => {
      //console.log(res)
      //2.绑定数据
      this.userInfo = res.data.data
      //3.将数据更新在vuex中
      this.$store.commit('updateuserInfo', res.data.data)
    })
  },
  methods: {
    logout () {
      this.$dialog
        .confirm({
          title: '你真要退出吗',
          message: '忍心残忍的离开我吗?'
        })
        .then(() => {
          // on confirm 确定的回调函数
          //清除token
          removeItem()
          //清除用户信息
          this.userInfo = {}
          //跳转首页
          this.$router.push({
            path: '/login'
          })
        })
        .catch(() => {
          // on cancel  取消的回调函数
        })
    }
  }
}
</script>

<style lang="less">
.container {
  //头部
  .info-box {
    height: 100px;
    background-color: #0098fa;
    display: flex;
    align-items: center;
    .van-image {
      padding: 0 10px;
    }
    .name {
      font-size: 14px;
      color: #fff;
      font-weight: normal;
    }
  }
  //layout布局 /* 操作导航 */
  .user-nav {
    padding: 15px 0;
    font-size: 12px;
    text-align: center;
    .van-icon {
      display: block;
      font-size: 24px;
      padding-bottom: 5px;
    }
  }
  //底部cell列表
}
</style>
